package com.lecampus.businessSecond_hand_transactions.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lecampus.businessSecond_hand_transactions.entity.Product;
import com.lecampus.businessSecond_hand_transactions.entity.ProductComment;
import com.lecampus.businessSecond_hand_transactions.mapper.ProductCommentMapper;
import com.lecampus.businessSecond_hand_transactions.mapper.ProductMapper;
import com.lecampus.businessSecond_hand_transactions.mapper.UserMapper;
import com.lecampus.businessSecond_hand_transactions.service.ProductCommentService;
import com.lecampus.businessSecond_hand_transactions.vo.ProductCommentVO;
import com.lecampus.businessSecond_hand_transactions.vo.UserVO;
import com.lecampus.commons.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-03-24
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductComment> implements ProductCommentService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCommentMapper productCommentMapper;
    @Autowired
    private UserMapper userMapper;

    private User user;
    private UserVO userVO;
    private ProductCommentVO productCommentVO;
    private List<ProductComment> productCommentList;
    private List<ProductCommentVO> productCommentVOList;

    private QueryWrapper<ProductComment> productCommentQueryWrapper;

    /**
     * 添加评论
     */
    @Override
    public void inputComment(String productId,ProductComment comment) {
        //添加评论
        productCommentMapper.insert(comment);

        //增加评论数
        Product product = productMapper.selectById(productId);
        product.setCommentSum(product.getCommentSum() + 1);
    }

    /**
     * 删除评论
     */
    @Override
    public void deleteByCommentId(String productId,String commentId) {
        //减少评论数
        Product product = productMapper.selectById(productId);
        product.setCommentSum(product.getCommentSum() - 1);

        //删除评论
        productCommentMapper.deleteById(commentId);
    }

    /**
     * 根据商品id查询评论
     */
    @Override
    public List<ProductCommentVO> findCommentsByProductId(String productId, long currentPage, long size) {
        //分页
        Page<ProductComment> page = new Page<>(currentPage,size);
        //条件
        productCommentQueryWrapper = new QueryWrapper<>();
        productCommentQueryWrapper.eq("product_id",productId);

        productCommentMapper.selectPage(page,productCommentQueryWrapper);
        productCommentList = page.getRecords();

        productCommentVOList = productCommentVOListUtil(productCommentList);

        return productCommentVOList;
    }

    /**
     * 封装评论
     */
    private ProductCommentVO productCommentVOUtil(ProductComment productComment) {
        user = userMapper.selectById(productComment.getUserId());
        System.out.println("user"+user);
        userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);

        productCommentVO = new ProductCommentVO();
        productCommentVO.setUserVO(userVO);
        BeanUtils.copyProperties(productComment,productCommentVO);

        return productCommentVO;
    }

    /**
     * 封装评论列表
     */
    private List<ProductCommentVO> productCommentVOListUtil(List<ProductComment> productCommentList) {
        productCommentVOList = new ArrayList<>();
        for (ProductComment productComment : productCommentList) {
            productCommentVOUtil(productComment);
            productCommentVOList.add(productCommentVO);
        }
        return productCommentVOList;
    }

    /**
     * 点赞
     */
    @Override
    public void likes(String productCommentId,int flag) {
        ProductComment productComment = productCommentMapper.selectById(productCommentId);

        //点赞
        if (flag == 0) {
            productComment.setLikeSum(productComment.getLikeSum() + 1);
        } else if (flag == 1 && productComment.getLikeSum() > 0) {
            //取消点赞
            productComment.setLikeSum(productComment.getLikeSum() - 1);
        }
        productCommentMapper.updateById(productComment);
    }
}
